package org.jeecg.modules.ws.controller;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.ws.common.WsFileEnum;
import org.jeecg.modules.ws.dto.UploadYdbgDto;
import org.jeecg.modules.ws.dto.WsCwbbDto;
import org.jeecg.modules.ws.entity.WsCompany;
import org.jeecg.modules.ws.entity.WsFile;
import org.jeecg.modules.ws.service.IWsCompanyService;
import org.jeecg.modules.ws.service.IWsFileService;
import org.jeecg.modules.ws.util.CwbbExcelUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 企业信息
 * @Author: jeecg-boot
 * @Date:   2025-04-15
 * @Version: V1.0
 */
@Slf4j
@Api(tags="企业信息")
@RestController
@RequestMapping("/wsCompany")
public class WsCompanyController extends JeecgController<WsCompany, IWsCompanyService> {
	@Autowired
	private IWsCompanyService wsCompanyService;
	@Autowired
	private CwbbExcelUtil cwbbExcelUtil;
	 @Autowired
	 private IWsFileService wsFileService;

	/**
	 * 分页列表查询
	 *
	 * @param wsCompany
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "企业信息-分页列表查询")
	@ApiOperation(value="企业信息-分页列表查询", notes="企业信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(WsCompany wsCompany,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LambdaQueryWrapper<WsCompany> query = Wrappers.lambdaQuery(WsCompany.class);
		String companyName = wsCompany.getCompanyName();
		if (StringUtils.isNotBlank(companyName)){
			query.like(WsCompany::getCompanyName, companyName);
		}
		query.eq(WsCompany::getType,wsCompany.getType());
		Page<WsCompany> page = new Page<WsCompany>(pageNo, pageSize);
		IPage<WsCompany> pageList = wsCompanyService.page(page, query);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param wsCompany
	 * @return
	 */
	@AutoLog(value = "企业信息-添加")
	@ApiOperation(value="企业信息-添加", notes="企业信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WsCompany wsCompany) {
		wsCompanyService.save(wsCompany);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param wsCompany
	 * @return
	 */
	@AutoLog(value = "企业信息-编辑")
	@ApiOperation(value="企业信息-编辑", notes="企业信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WsCompany wsCompany) {
		wsCompanyService.updateById(wsCompany);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业信息-通过id删除")
	@ApiOperation(value="企业信息-通过id删除", notes="企业信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		wsCompanyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业信息-批量删除")
	@ApiOperation(value="企业信息-批量删除", notes="企业信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wsCompanyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业信息-通过id查询")
	@ApiOperation(value="企业信息-通过id查询", notes="企业信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		WsCompany wsCompany = wsCompanyService.getById(id);
		return Result.OK(wsCompany);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param wsCompany
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, WsCompany wsCompany) {
      return super.exportXls(request, wsCompany, WsCompany.class, "企业信息");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, WsCompany.class);
  }
	 /**
	  * 上传财务报表数据
	  *
	  * @param file
	  * @return
	  */
	 @PostMapping(value = "/uploadCwbbExcel")
	 public Result<?> uploadCwbbExcel(@RequestParam(name="file",required=true) MultipartFile file, HttpServletRequest request) {
		 try {
			 String shxydm = request.getHeader("shxydm");
			 String companyName = URLDecoder.decode(request.getHeader("companyName"), "UTF-8");
			 String skssqq = request.getHeader("skssqq");
			 String skssqz = request.getHeader("skssqz");
			 WsCwbbDto dto = new WsCwbbDto();
			 dto.setCompanyName(companyName);
			 dto.setShxydm(shxydm);
			 dto.setSkssqq(DateUtil.parse(skssqq, "yyyy-MM-dd"));
			 dto.setSkssqz(DateUtil.parse(skssqz, "yyyy-MM-dd"));
			 cwbbExcelUtil.importCwbb(file.getInputStream(), dto);
		 } catch (Exception e) {
			 log.error("导入财务报表数据异常{}", e.getMessage(), e);
			 return Result.error("导入财务报表数据异常");
		 }
		 return Result.OK();
	 }

	 /**
	  * 上传月度报告数据
	  *
	  * @param file
	  * @return
	  */
	 @PostMapping(value = "/uploadYdbgExcel")
	 public Result<?> uploadYdbgExcel(@RequestParam(name="file",required=true) MultipartFile file, HttpServletRequest request) {
		 try {
			 String shxydm = request.getHeader("shxydm");
			 String companyName = URLDecoder.decode(request.getHeader("companyName"), "UTF-8");
			 String year = request.getHeader("year");
			 String month = request.getHeader("month");
			 UploadYdbgDto dto = new UploadYdbgDto();
			 dto.setCompanyName(companyName);
			 dto.setShxydm(shxydm);
			 dto.setYear(year);
			 dto.setMonth(month);
			 WsCompany wsCompany = cwbbExcelUtil.importYdbg(file.getInputStream(), dto);
			 String bizTableId = wsCompany.getId();
			 WsFile wsFile = wsFileService.uploadFile(file, WsFileEnum.WS_YDBG.getBizName(), bizTableId, WsFileEnum.WS_YDBG.getBizFieldName());
			 wsCompany.setFileId(wsFile.getId());
			 wsCompanyService.updateById(wsCompany);
		 } catch (Exception e) {
			 log.error("导入财务报表数据异常{}", e.getMessage(), e);
			 return Result.error("导入财务报表数据异常");
		 }
		 return Result.OK();
	 }
}
